<?php


namespace app\controllers;

use Yii;
use yii\filters\AccessControl;
use yii\filters\VerbFilter;
use yii\helpers\ArrayHelper;
use yii\helpers\Json;
use yii\web\Controller;

class BaseController extends Controller {
    protected $Params = [];
    protected $tokenKey = 'Users:Token';
    protected $mobileKey = 'Users:Mobile';
    public $enableCsrfValidation = false;

    public function __construct($id, $module, $config = []) {
        parent::__construct($id, $module, $config);
    }

    public function init() {
        parent::init();
        $this->request();
    }

//    public function behaviors() {
//        return [
//            'access' => [
//                'class' => AccessControl::className(),
//                'only' => ['logout'],
//                'rules' => [
//                    [
//                        'actions' => ['logout'],
//                        'allow' => true,
//                        'roles' => ['@'],
//                    ],
//                ],
//            ],
//            'verbs' => [
//                'class' => VerbFilter::className(),
//                'actions' => [
//                    'logout' => ['get'],
//                ],
//            ],
//        ];
//    }

    /*
   * 获取参数
   * **/
    public function request() {
        $request = Yii::$app->request;
        $GET = $request->get();
        if (empty($this->Params)) {
            array_shift($GET);
            $Params = ArrayHelper::merge($request->post(), $GET);
        }

        foreach ($Params as $key => $value) {
            if (empty($value)) {
                $this->outJson([], ERROR_CODE, $key . '参数为空');
                Yii::$app->end();
            }
        }
        $this->Params = $Params;
    }

    /*
   * 返回json
   * **/
    public function outJson($data = [], $code = SUCCESS_CODE, $msg = 'successfully!', $version = '1.0') {
        $Response = (object)[];
        $Response->code = $code;
        $Response->msg = $msg;
        $Response->data = $data;
        $Response->version = $version;
        $this->exportJson($Response);
    }

    /*
    * json 格式
    * */
    protected function exportJson($data) {
        $originHost = empty($_SERVER['HTTP_ORIGIN']) ? '' : $_SERVER['HTTP_ORIGIN'];
        $protocol = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off' || $_SERVER['SERVER_PORT'] == 443) ? "https://" : "http://";
        $allowOrigin = [
            'http://www.zwshop.com.cn',
            'http://info.farmhappy.info/login/index',
        ];
        if (empty($originHost) || $originHost == $protocol . $_SERVER['HTTP_HOST']) {
            $this->_exportJson($data);
        } else {
            if (in_array($originHost, $allowOrigin))
                $this->_exportJson($data, $originHost);
            else
                $this->_exportJsonp($data);
        }
    }

    private function _exportJson($data, $originHost = null) {
        if (!empty($originHost))
            header('Access-Control-Allow-Origin:' . $originHost);

        $response = Yii::$app->response;
        $response->format = \yii\web\Response::FORMAT_JSON;
        $response->headers->set('Pragma', 'no-cache');
        $response->data = $data;
        header('Cache-Control: no-cache, no-store, max-age=0');
        header('Expires: 1L');
        Yii::$app->end();
    }

    private function _exportJsonp($data) {
//        $callback = $_GET['callback'];
//        echo $callback . '(' . Json::encode($data) . ')';
        echo Json::encode($data);
        Yii::$app->end();
    }

}